package com.example.simplerssreader;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class SimpleRssReaderActivity extends ListActivity 	{
	/** Called when the activity is first created. */

	List headlines;
	List links;
	Button button;
	String text;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		initializeVars();

	try {
		text = "http://feeds.feedburner.com/Creativemonkeyz";
			URL url = new URL(text);
			// http://feeds.feedburner.com/Creativemonkeyz
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

			factory.setNamespaceAware(false);
			XmlPullParser xpp = factory.newPullParser();

			xpp.setInput(getInputStream(url), "UTF_8");

			boolean insideItem = false;

			int eventType = xpp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equalsIgnoreCase("item")) {

						insideItem = true;
					} else if (xpp.getName().equalsIgnoreCase("title")) {
						if (insideItem)
							headlines.add(xpp.nextText()); // headline
					} else if (xpp.getName().equalsIgnoreCase("link")) {
						if (insideItem)
							links.add(xpp.nextText());// Link
					}

				} else if (eventType == XmlPullParser.END_TAG
						&& xpp.getName().equalsIgnoreCase("item")) {
					insideItem = false;
				}

				eventType = xpp.next(); // next element

			}

		} catch (MalformedURLException e) {

			e.printStackTrace();

		} catch (XmlPullParserException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

		// Binding data

		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, headlines);

		setListAdapter(adapter);    

	}

	private InputStream getInputStream(URL url) {
		// TODO Auto-generated method stub
		try {
			return url.openConnection().getInputStream();
		} catch (IOException e) {
			return null;
		}
	}

	private void initializeVars() {
		// TODO Auto-generated method stub
		headlines = new ArrayList();
		links = new ArrayList();
	//	text = (EditText) findViewById(R.id.etUrl);
		button = (Button) findViewById(R.id.bGetrss);
		//button.setOnClickListener(this);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		Uri uri = Uri.parse((String) links.get(position));
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);

	}

     public void onClick(View v) {
		// TODO Auto-generated method stub

		if (v.getId() == R.id.bGetrss) {
			try {
				URL url = new URL(text.toString());
				// http://feeds.feedburner.com/Creativemonkeyz
				XmlPullParserFactory factory = XmlPullParserFactory
						.newInstance();

				factory.setNamespaceAware(false);
				XmlPullParser xpp = factory.newPullParser();

				xpp.setInput(getInputStream(url), "UTF_8");

				boolean insideItem = false;

				int eventType = xpp.getEventType();
				while (eventType != XmlPullParser.END_DOCUMENT) {
					if (eventType == XmlPullParser.START_TAG) {

						if (xpp.getName().equalsIgnoreCase("item")) {

							insideItem = true;
						} else if (xpp.getName().equalsIgnoreCase("title")) {
							if (insideItem)
								headlines.add(xpp.nextText()); // headline
						} else if (xpp.getName().equalsIgnoreCase("link")) {
							if (insideItem)
								links.add(xpp.nextText());// Link
						}

					} else if (eventType == XmlPullParser.END_TAG
							&& xpp.getName().equalsIgnoreCase("item")) {
						insideItem = false;
					}

					eventType = xpp.next(); // next element

				}

			} catch (MalformedURLException e) {

				e.printStackTrace();

			} catch (XmlPullParserException e) {

				e.printStackTrace();

			} catch (IOException e) {

				e.printStackTrace();

			}

		}

		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, headlines);

		setListAdapter(adapter);
     		
	} 

}